﻿$(function () {
    chrome.extension.sendRequest({}, function (response) {

        var siteConfig = {
            'www.360buy.com': {
                regex: /http:\/\/www\.360buy\.com\/product\/([0-9]+)\.html/,
                union: 'http://click.union.360buy.com/JdClick/?unionId=5990&t=4&to=${url}'
            },
            'dangdang.com': {
                regex: /http:\/\/product\.dangdang\.com\/product\.aspx\?product_id=([0-9]+)/,
                union: '{url}'
            },
            "99read.com": {
                regex: /http:\/\/product\.dangdang\.com\/product\.aspx\?product_id=([0-9]+)/,
                union: '{url}'
            },
            "amazon.cn": {
                regex: /http:\/\/product\.dangdang\.com\/product\.aspx\?product_id=([0-9]+)/,
                union: '{url}'
            },
            "beifabook.com": {
                regex: /http:\/\/product\.dangdang\.com\/product\.aspx\?product_id=([0-9]+)/,
                union: '{url}'
            },
            "bookschina.com": {
                regex: /http:\/\/product\.dangdang\.com\/product\.aspx\?product_id=([0-9]+)/,
                union: '{url}'
            },
            "bookuu.com": {},
            "china-pub.com": {},
            "coo8.com": {},
            "dangdang.com": {
                regex: /http:\/\/product\.dangdang\.com\/product\.aspx\?product_id=([0-9]+)/,
                union: '{url}'
            },
            "newegg.com.cn": {
                regex: /http:\/\/product\.dangdang\.com\/product\.aspx\?product_id=([0-9]+)/,
                union: '{url}'
            },
            "icson.com": {},
            "51buy.com": {},
            "suning.com": {
                regex: /http:\/\/product\.dangdang\.com\/product\.aspx\?product_id=([0-9]+)/,
                union: '{url}'
            },
            "taobao.com": {
                regex: /http:\/\/product\.dangdang\.com\/product\.aspx\?product_id=([0-9]+)/,
                union: '{url}'
            },
            "tmall.com": {
                regex: /http:\/\/product\.dangdang\.com\/product\.aspx\?product_id=([0-9]+)/,
                union: '{url}'
            }

        };

        function url2key(domain, url) {
            var that = this;
            if (arguments.length == 1) {
                url = domain;
                domain = null;
            }
            if (urlReg(url))
                return key;
            return null;
        }

        function urlReg(site, url) {
            if (siteConfig[site].regex) {
                var r = url.match(siteConfig[site].regex);
                if (r && r.length == 2) {
                    return {
                        key: site,
                        id: r[1]
                    };
                }
            }
            return null;
        }

        $('a').click(function () {
            var url = $(this).attr('href');
            var site = $.url(url).attr('host');
            if (urlReg(site, url)) {
                var tmpl = $.template(null, siteConfig[site].union);
                var unionUrl = $.tmpl(tmpl, { 'url': url }).text();

                $(this).attr('href', unionUrl);

                chrome.extension.sendRequest({ action: "getUserId" }, function (res) {
                    var userId = res.userId;
                    alert(userId);
                    if (userId) {
                        $.ajax({
                            type: 'GET',
                            //url: 'http://www.1diantao.com/1diantao/ajax.php',
                            url: 'http://1diantao.sinaapp.com/1diantao/ajax.php',
                            data: {
                                n: userId,
                                u: unionUrl
                            },
                            success: function (res) {
                                alert(res);
                            },
                            error: function (xhr) {
                                alert(xhr);
                            }
                        });
                    }
                });
            }
        });
    });
});